// 页面加载动画
document.addEventListener('DOMContentLoaded', function() {
    const loading = document.querySelector('.loading');
    if (loading) {
        setTimeout(() => {
            loading.style.opacity = '0';
            setTimeout(() => {
                loading.remove();
            }, 500);
        }, 500);
    }
});

// 表单验证
function validateForm(form) {
    const inputs = form.querySelectorAll('input[required]');
    let isValid = true;

    inputs.forEach(input => {
        if (!input.value.trim()) {
            isValid = false;
            input.classList.add('is-invalid');
        } else {
            input.classList.remove('is-invalid');
        }
    });

    return isValid;
}

// 密码强度检查
function checkPasswordStrength(password) {
    const strength = {
        length: password.length >= 8,
        hasUpper: /[A-Z]/.test(password),
        hasLower: /[a-z]/.test(password),
        hasNumber: /[0-9]/.test(password),
        hasSpecial: /[!@#$%^&*]/.test(password)
    };

    const strengthScore = Object.values(strength).filter(Boolean).length;
    return strengthScore;
}

// 动态更新密码强度指示器
function updatePasswordStrength(password, strengthIndicator) {
    const strength = checkPasswordStrength(password);
    const strengthText = ['弱', '中', '强', '很强', '非常强'];
    const strengthColors = ['#e74c3c', '#f39c12', '#3498db', '#2ecc71', '#27ae60'];

    strengthIndicator.textContent = strengthText[strength - 1];
    strengthIndicator.style.color = strengthColors[strength - 1];
}

// 验证码刷新
function refreshCaptcha() {
    const captchaImg = document.querySelector('.captcha-container img');
    if (captchaImg) {
        captchaImg.src = '/captcha?' + new Date().getTime();
    }
}

// 添加表单提交动画
document.querySelectorAll('form').forEach(form => {
    form.addEventListener('submit', function(e) {
        if (!validateForm(this)) {
            e.preventDefault();
            return;
        }

        const submitBtn = this.querySelector('button[type="submit"]');
        if (submitBtn) {
            submitBtn.innerHTML = '<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> 处理中...';
            submitBtn.disabled = true;
        }
    });
});

// 添加输入框动画效果
document.querySelectorAll('.form-control').forEach(input => {
    input.addEventListener('focus', function() {
        this.parentElement.classList.add('focused');
    });

    input.addEventListener('blur', function() {
        if (!this.value) {
            this.parentElement.classList.remove('focused');
        }
    });
}); 